package com.lzh.user.controller;


import com.lzh.common.pojo.Result;
import com.lzh.core.controller.AbstractCoreController;

import com.lzh.user.entity.ApUser;
import com.lzh.user.service.ApUserService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
* <p>
* APP用户信息表 控制器</p>
* @author lzh
* @since 2021-06-24
*/
@RestController
@RequestMapping("/userList")
public class ApUserController extends AbstractCoreController<ApUser> {

    private ApUserService apUserService;

    //注入
    @Autowired
    public ApUserController(ApUserService apUserService) {
        super(apUserService);
        this.apUserService = apUserService;
    }

    @ApiOperation(value = "用户注册")
    @PostMapping("/register")
    public Result register(@RequestBody ApUser apUser) {
        boolean result = apUserService.register(apUser);
        if (result) {
        return Result.ok(apUser.getName()+ "注册成功！");
        } else return Result.error("注册失败！");
    }

    @ApiOperation(value = "用户登录")
    @PostMapping("/login")
    public Result login(@RequestBody ApUser apUser) {
        if (StringUtils.isEmpty(apUser) || StringUtils.isEmpty(apUser.getName()) || StringUtils.isEmpty(apUser.getPassword())) {
            return Result.errorMessage("用户名或密码不能为空！");
        }
        Map<String,Object> userInfo = apUserService.login(apUser);
        if (userInfo == null) {
            return Result.errorMessage("登录失败！请检查用户名或密码是否正确！");
        }
        return Result.ok(userInfo);
    }

}

